
import 'package:cunyutech_flutter_ui/src/funcs/main.dart';
import 'package:cunyutech_flutter_ui/src/text/main.dart';
import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

enum SystemAlertActionType {
  cancelBtn,
  primaryBtn
}

class SystemAlertAction {
  final String title;
  final SystemAlertActionType type;

  SystemAlertAction(this.title, [this.type = SystemAlertActionType.primaryBtn]);
}

Future<int> showSystemAlert({
    BuildContext rootContext, 
    String title,
    Widget child, 
    List<SystemAlertAction> actions,
    bool barrierDismissible,
  }) {
    var cs = CYThemeData.of(rootContext);
    var builder = cyThemeWrapBuilder((BuildContext context) {
        Widget content =  SystemAlertDialog(
          title: title == null ? null : CYTitle(title, alignment: Alignment.center, textAlign: TextAlign.center,),
          actions: actions.map((item){
            return CupertinoDialogAction(
              child: CYParagraph(item.title, color: cs.linkColor, alignment: Alignment.center,),
              isDestructiveAction: item.type == SystemAlertActionType.cancelBtn,
              isDefaultAction: item.type == SystemAlertActionType.primaryBtn,
              onPressed: () {
                Navigator.of(rootContext).pop(actions.indexOf(item));
              },
            );
          }).toList(),
          child: child,
        );
        if(barrierDismissible == false) {
          content = ClickTwiceBackToHome(
            child: Scaffold(
              backgroundColor: Colors.transparent,
              body: content,
            )
          );
        }
        return content;
      }, rootContext);

    return showCupertinoDialog(
      builder: builder, 
      context: rootContext
    );
  }



class SystemAlertDialog extends StatefulWidget {
  SystemAlertDialog({Key key, this.title, this.child, this.actions }) : super(key: key);

  final Widget title;
  final Widget child;
  final List<CupertinoDialogAction> actions;


  @override
  _SystemAlertDialogState createState() => _SystemAlertDialogState();
}

class _SystemAlertDialogState extends State<SystemAlertDialog> {

  static double _titleVertical = 26;
  static double _titleHorizontal = CYMargin.Default;

static const TextStyle _cupertinoDialogTitleStyle = TextStyle(
  fontFamily: '.SF UI Display',
  inherit: false,
  fontSize: CYFontSize.Large,
  letterSpacing: 0.48,
  textBaseline: TextBaseline.alphabetic,
);

static const TextStyle _cupertinoDialogContentStyle = TextStyle(
  fontFamily: '.SF UI Text',
  inherit: false,
  fontSize: CYFontSize.Default,
  height: 1.036,
  letterSpacing: -0.25,
  textBaseline: TextBaseline.alphabetic,
);

static const TextStyle _cupertinoDialogActionStyle = TextStyle(
  fontFamily: '.SF UI Text',
  inherit: false,
  fontSize: CYFontSize.Large,
  color: CupertinoColors.activeBlue,
  textBaseline: TextBaseline.alphabetic,
);

buildChild() {
  if(widget.child == null) return Container();
  return Padding(
    padding: EdgeInsets.only(
      left: _titleHorizontal,
      right: _titleHorizontal,
      bottom: _titleVertical,
      top: widget.title == null ? _titleVertical : 1.0,
    ),
    child: DefaultTextStyle(
      style: _cupertinoDialogContentStyle,
      textAlign: TextAlign.center,
      child: widget.child,
    ),
  );
}

  buildTitle() {
    if(widget.title == null) return Container();
    return Padding(
      padding: EdgeInsets.only(
        left: _titleHorizontal,
        right: _titleHorizontal,
        bottom: widget.child == null ? _titleVertical : 1.0,
        top: _titleVertical,
      ),
      child: DefaultTextStyle(
        style: _cupertinoDialogTitleStyle,
        textAlign: TextAlign.center,
        child: widget.title,
      ),
    );
  }

  buildActions() {
    var cs = CYThemeData.of(context);
    return Container(
      height: CYHeight.Large,
      decoration: BoxDecoration(
        border: Border(top: BorderSide(color: cs.foreColorSystem.borderColor))
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: widget.actions.map((item){
          bool isLast = widget.actions.last == item;
          return Expanded(
              flex: 1,
              child: GestureDetector(
                onTap: item.onPressed,
                child: Container(
                  decoration: BoxDecoration(
                    border: isLast ? null : Border(right: BorderSide(color: cs.foreColorSystem.borderColor))
                  ),
                  child: DefaultTextStyle(
                    style: _cupertinoDialogActionStyle.merge(item.textStyle),
                    textAlign: TextAlign.center,
                    child: item.child
                  )
              ),
            ),
          );
        }).toList()
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    var cs = CYThemeData.of(context);
    return Container(
      alignment: Alignment.center,
      child: Container(
       width: CYScreen.width*2/3,
       decoration: BoxDecoration(
         color: cs.backColorSystem.secondaryColor,
         borderRadius: BorderRadius.circular(20)
       ),
       child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            buildTitle(),
            buildChild(),
            buildActions(),
          ]
        ),
      )
    );
  }
}

class SystemConfirmActionTitle extends StatelessWidget {
  const SystemConfirmActionTitle(this.title, {Key key }) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return CYParagraph(title, alignment: Alignment.center, lineHeight: null, fontSize: CYFontSize.Large);
  }
}
